package com.yeshine.opendata.controller.front.my.favorites;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.yeshine.opendata.controller.front.AbstractFrontControllerBase;
import com.yeshine.opendata.entity.Favorites;
import com.yeshine.opendata.entity.Favorites_list;
import com.yeshine.opendata.service.IFavoritesService;
import com.yeshine.opendata.service.IFavorites_listService;
import com.yeshine.opendata.utils.PageResult;
import com.yeshine.opendata.utils.Result;

/**
 * Created by taohj on 2014/7/15.
 */
@Controller
@RequestMapping("/front/my/favorites")
public class MyFavoritesController extends AbstractFrontControllerBase {

    @Autowired
    private IFavoritesService service;
    @Autowired
    private IFavorites_listService servicelist;
    @RequestMapping("createV")
    public String createV() {
        return "front/my/favorites/create";
    }

    @RequestMapping("create")
    public String crate(
            @RequestParam("name") String name,
            @RequestParam("description") String description,
            @RequestParam("image") MultipartFile image
    ) {
        Favorites favorites = new Favorites();
        favorites.setName(name);
        favorites.setDescription(description);
        favorites.setHeadImageFile(image);
        favorites.setCreater(getCurrentUser());
        favorites.setCreateTime(new Date());
       

        Result<Favorites> result = service.create(favorites,getRequest());
        return getReturnString(result, "redirect:/front/my/favorites/list", setMessage("创建失败", result.getMessage()));
    }

    @RequestMapping("remove")
    public String remove(
            @RequestParam("id") Integer id
    ) {
        Result<Favorites> result = service.remove(new Favorites(id));
        return getReturnString(result, "redirect:/front/my/favorites/list", setMessage("删除失败", result.getMessage()));
    }

    @RequestMapping("modifyV")
    public String modifyV(
            @RequestParam("id") Integer id
    ) {
        Favorites favorites = service.getById(id).getTarget();
        setRequestAttr("favorites", favorites);
        System.out.println("imga----"+favorites.getHeadImage());
        System.out.println("de----"+favorites.getDescription());
        return "front/my/favorites/modify";
    }

    @RequestMapping("modify")
    public String modify(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("image") MultipartFile image,
            @RequestParam("description") String description
            
    ) {
    	
        Favorites favorites = new Favorites(id);
        favorites.setName(name);
        favorites.setDescription(description);
        favorites.setHeadImageFile(image);
       
        Result<Favorites> result = service.modify(favorites,getRequest());
        return getReturnString(result, "redirect:/front/my/favorites/list", setMessage("操作失败", "操作失败请联系管理员"));
    }


    @RequestMapping("list")
    public String list(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "size", required = false) Integer size

    ) {

        PageResult<Favorites> pageResult = new PageResult<Favorites>(page, size);
        Favorites favorites = new Favorites();
        //favorites.setCreater(getCurrentUser());
        pageResult.setT(favorites);
        Result<Favorites> result = service.getByPage(pageResult);
        setRequestAttr("result", result);
        return "front/my/favorites/list";
    }
    
    @RequestMapping("detailList")
    public String detailList(
    		@RequestParam("fid") Integer fid
    		){
    	   Favorites favorites = new Favorites();
       
           Result<Favorites> result = service.getList(favorites);
     
           setRequestAttr("result", result);
           
           Favorites_list favorites_list=new Favorites_list();
           favorites_list.setFavorites(new Favorites(fid));
           
           Result<Favorites_list> resultlist = servicelist.getList(favorites_list);
           setRequestAttr("resultlist", resultlist);
    	return "front/my/favorites/detaillist";
    	
    }


}
